﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;

namespace MES
{
    public class SendEvent
    {

        public static byte[] UTILITY_DATA_REPORT_ICT(Device device, string airCylinder_Count, string inPCB_Motor, string outPCB_Motor,string NumberSystem,string sorting ,out string MSG, out string transactionID)
        {
            Random random = new Random();
            string ip = device.TCPClient.ClientSocket.LocalEndPoint.ToString();
            transactionID = DateTime.Now.ToString("yyyyMMddHHmmssfffff") + random.Next(10000, 99999).ToString();
            string Header =
                "<Message>" +
                "<Header>" +
                "<MESSAGENAME>UTILITY_DATA_REPORT</MESSAGENAME>" +
                "<TRANSACTIONID>" + transactionID + "</TRANSACTIONID>" +
                "<REPLYSUBJECTNAME>" + ip + "</REPLYSUBJECTNAME>" +
                "</Header>";

            string Body =
                "<Body>" +
                "<EquipmentID>" + device.DeviceID + "</EquipmentID>" +
                "<UtilityDataList>" +
                "<UtilityData><Name>airCylinder_Count</Name><Value>" + airCylinder_Count + "</Value></UtilityData>" +
                "<UtilityData><Name>inPCB_Motor</Name><Value>" + inPCB_Motor+ "</Value></UtilityData>" +
                "<UtilityData><Name>outPCB_Motor</Name><Value>" + outPCB_Motor + "</Value></UtilityData>" +
                "<UtilityData><Name>NumberSystem</Name><Value>" + NumberSystem + "</Value></UtilityData>" +
                "<UtilityData><Name>sorting</Name><Value>" + sorting + "</Value></UtilityData>" +
                "</UtilityDataList>" +
                "</Body>";
            string Return =
                "<Return>" +
                "<ReturnCode></ReturnCode>" +
                "<ReturnMessage></ReturnMessage>" +
                "</Return>" +
                "</Message>";

            MSG = (Header + Body + Return).Replace(" ", "");
            MSG = FormatXML(MSG);
            byte[] buffer = Encoding.UTF8.GetBytes(MSG);

            string num = Convert.ToString(buffer.Length, 16);
            string head = string.Empty;
            for (int i = 0; i < 8 - num.Length; i++)
            {
                head += "0";
            }
            head += num.ToUpper();

            //定义一个string类型的变量，用于存储转换后的值。
            string resultMSG = string.Empty;

            for (int i = 0; i < buffer.Length; i++)
            {
                //将每一个字节数组转换成16进制的字符串，以空格相隔开。
                string convert = Convert.ToString(buffer[i], 16);
                if (convert.Length == 1)
                {
                    convert = convert.Insert(0, "0");

                }
                resultMSG += convert.ToUpper();
            }
            resultMSG = head + resultMSG;

            byte[] sendByte = ConvertHexStrToByteArray(resultMSG);

            return sendByte;

        }

        public static byte[] Operator_Login_Logout(Device device, string LogState, out string MSG, out string transactionID)
        {
            Random random = new Random();
            string ip = device.TCPClient.ClientSocket.LocalEndPoint.ToString();
            transactionID = DateTime.Now.ToString("yyyyMMddHHmmssfffff") + random.Next(10000, 99999).ToString();
            string Header =
                "<Message>" +
                "<Header>" +
                "<MESSAGENAME>OPERATOR_LOGIN_LOGOUT_REPORT</MESSAGENAME>" +
                "<TRANSACTIONID>" + transactionID + "</TRANSACTIONID>" +
                "<REPLYSUBJECTNAME>" + ip + "</REPLYSUBJECTNAME>" +
                "</Header>";

            string Body =
                "<Body>" +
                "<EquipmentID>" + device.DeviceID + "</EquipmentID>" +
                "<OperatorID>" + LogState + "</OperatorID>" +
                "</Body>";
            string Return =
                "<Return>" +
                "<ReturnCode></ReturnCode>" +
                "<ReturnMessage></ReturnMessage>" +
                "</Return>" +
                "</Message>";

            MSG = (Header + Body + Return).Replace(" ", "");
            MSG = FormatXML(MSG);
            byte[] buffer = Encoding.UTF8.GetBytes(MSG);

            string num = Convert.ToString(buffer.Length, 16);
            string head = string.Empty;
            for (int i = 0; i < 8 - num.Length; i++)
            {
                head += "0";
            }
            head += num.ToUpper();

            //定义一个string类型的变量，用于存储转换后的值。
            string resultMSG = string.Empty;

            for (int i = 0; i < buffer.Length; i++)
            {
                //将每一个字节数组转换成16进制的字符串，以空格相隔开。
                string convert = Convert.ToString(buffer[i], 16);
                if (convert.Length == 1)
                {
                    convert = convert.Insert(0, "0");

                }
                resultMSG += convert.ToUpper();
            }
            resultMSG = head + resultMSG;

            byte[] sendByte = ConvertHexStrToByteArray(resultMSG);

            return sendByte;

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="device"></param>
        /// <param name="EQP_Status"></param>
        /// <param name="MSG"></param>
        /// <param name="transactionID"></param>
        /// <returns></returns>
        public static byte[] CIM_CONTROL_MODE_COMMAND(Device device, string ControlMode, out string MSG, out string transactionID)
        {
            Random random = new Random();
            string ip = device.TCPClient.ClientSocket.LocalEndPoint.ToString();
            transactionID = DateTime.Now.ToString("yyyyMMddHHmmssfffff") + random.Next(10000, 99999).ToString();
            string Header =
                "<Message>" +
                "<Header>" +
                "<MESSAGENAME>CIM_CONTROL_MODE_COMMAND</MESSAGENAME>" +
                "<TRANSACTIONID>" + transactionID + "</TRANSACTIONID>" +
                "<REPLYSUBJECTNAME>" + ip + "</REPLYSUBJECTNAME>" +
                "</Header>";

            string Body =
                "<Body>" +
                "<EquipmentID>" + device.DeviceID + "</EquipmentID>" +
                "<CimControlMode>" + ControlMode + "</CimControlMode>" +
                "</Body>";
            string Return =
                "<Return>" +
                "<ReturnCode></ReturnCode>" +
                "<ReturnMessage></ReturnMessage>" +
                "</Return>" +
                "</Message>";

            MSG = (Header + Body + Return).Replace(" ", "");
            MSG = FormatXML(MSG);
            byte[] buffer = Encoding.UTF8.GetBytes(MSG);

            string num = Convert.ToString(buffer.Length, 16);
            string head = string.Empty;
            for (int i = 0; i < 8 - num.Length; i++)
            {
                head += "0";
            }
            head += num.ToUpper();

            //定义一个string类型的变量，用于存储转换后的值。
            string resultMSG = string.Empty;

            for (int i = 0; i < buffer.Length; i++)
            {
                //将每一个字节数组转换成16进制的字符串，以空格相隔开。
                string convert = Convert.ToString(buffer[i], 16);
                if (convert.Length == 1)
                {
                    convert = convert.Insert(0, "0");

                }
                resultMSG += convert.ToUpper();
            }
            resultMSG = head + resultMSG;

            byte[] sendByte = ConvertHexStrToByteArray(resultMSG);

            return sendByte;

        }

        /// <summary>
        /// 设备状态发送
        /// </summary>
        /// <param name="device">设备ID</param>
        /// <param name="EQP_Status">设备当前状态</param>
        /// <param name="MSG">上传MES的字符串</param>
        /// <returns></returns>
        public static byte[] EQP_STATUS_REPORT(Device device, string EQP_Status, out string MSG, out string transactionID)
        {
            Random random = new Random();
            string ip = device.TCPClient.ClientSocket.LocalEndPoint.ToString();
            transactionID = DateTime.Now.ToString("yyyyMMddHHmmssfffff") + random.Next(10000, 99999).ToString();
            string Header =
                "<Message>" +
                "<Header>" +
                "<MESSAGENAME>EQP_STATUS_REPORT</MESSAGENAME>" +
                "<TRANSACTIONID>" + transactionID + "</TRANSACTIONID>" +
                "<REPLYSUBJECTNAME>" + ip + "</REPLYSUBJECTNAME>" +
                "</Header>";

            string Body =
                "<Body>" +
                "<EquipmentID>" + device.DeviceID + "</EquipmentID>" +
                "<EquipmentStatus>" + EQP_Status + "</EquipmentStatus>" +
                "</Body>";
            string Return =
                "<Return>" +
                "<ReturnCode></ReturnCode>" +
                "<ReturnMessage></ReturnMessage>" +
                "</Return>" +
                "</Message>";

            MSG = (Header + Body + Return).Replace(" ", "");
            MSG = FormatXML(MSG);
            byte[] buffer = Encoding.UTF8.GetBytes(MSG);

            string num = Convert.ToString(buffer.Length, 16);
            string head = string.Empty;
            for (int i = 0; i < 8 - num.Length; i++)
            {
                head += "0";
            }
            head += num.ToUpper();

            //定义一个string类型的变量，用于存储转换后的值。
            string resultMSG = string.Empty;

            for (int i = 0; i < buffer.Length; i++)
            {
                //将每一个字节数组转换成16进制的字符串，以空格相隔开。
                string convert = Convert.ToString(buffer[i], 16);
                if (convert.Length == 1)
                {
                    convert = convert.Insert(0, "0");

                }
                resultMSG += convert.ToUpper();
            }
            resultMSG = head + resultMSG;

            byte[] sendByte = ConvertHexStrToByteArray(resultMSG);

            return sendByte;

        }

        /// <summary>
        /// 设备报警发送
        /// </summary>
        /// <param name="device">设备ID</param>
        /// <param name="AlarmStatus">报警发生&解除</param>
        /// <param name="AlarmLevel">报警等级</param>
        /// <param name="AlarmCode">报警内容编号</param>
        /// <param name="AlarmText">报警内容</param>
        /// <param name="MSG">MES上传字符串</param>
        /// <returns></returns>
        public static byte[] ALARM_REPORT(Device device, int AlarmStatus, int AlarmLevel, int AlarmCode, string AlarmText, out string MSG, out string transactionID)
        {
            Random random = new Random();
            string ip = device.TCPClient.ClientSocket.LocalEndPoint.ToString();
            transactionID = DateTime.Now.ToString("yyyyMMddHHmmssfffff") + random.Next(10000, 99999).ToString();
            string Header =
                "<Message>" +
                "<Header>" +
                "<MESSAGENAME>ALARM_REPORT</MESSAGENAME>" +
                "<TRANSACTIONID>" + transactionID + "</TRANSACTIONID>" +
                "<REPLYSUBJECTNAME>" + ip + "</REPLYSUBJECTNAME>" +
                "</Header>";
            string Body =
                "<Body>" +
                "<EquipmentID> " + device.DeviceID + "</EquipmentID> " +
                "<AlarmStatus>" + AlarmStatus.ToString() + "</AlarmStatus>" +
                "<AlarmLevel>" + AlarmLevel.ToString() + "</AlarmLevel>" +
                "<AlarmCode>" + AlarmCode.ToString() + "</AlarmCode>" +
                "<AlarmText>" + AlarmText + "</AlarmText>" +
                "</Body>";
            string Return =
                "<Return>" +
                "<ReturnCode></ReturnCode>" +
                "<ReturnMessage></ReturnMessage>" +
                "</Return>" +
                "</Message>";

            MSG = (Header + Body + Return).Replace(" ", "");
            MSG = FormatXML(MSG);
            byte[] buffer = Encoding.UTF8.GetBytes(MSG);

            string num = Convert.ToString(buffer.Length, 16);
            string head = string.Empty;
            for (int i = 0; i < 8 - num.Length; i++)
            {
                head += "0";
            }
            head += num.ToUpper();

            //定义一个string类型的变量，用于存储转换后的值。
            string resultMSG = string.Empty;

            for (int i = 0; i < buffer.Length; i++)
            {
                //将每一个字节数组转换成16进制的字符串，以空格相隔开。
                string convert = Convert.ToString(buffer[i], 16);
                if (convert.Length == 1)
                {
                    convert = convert.Insert(0, "0");

                }
                resultMSG += convert.ToUpper();
            }
            resultMSG = head + resultMSG;

            byte[] sendByte = ConvertHexStrToByteArray(resultMSG);

            return sendByte;

        }

        /// <summary>
        /// 确认连接
        /// </summary>
        /// <param name="MSG"></param>
        /// <returns></returns>
        public static byte[] EAP_ALIVE_CHECK(Device device, out string MSG, out string transactionID)
        {
            Random random = new Random();
            string ip = device.TCPClient.ClientSocket.LocalEndPoint.ToString();
            transactionID = DateTime.Now.ToString("yyyyMMddHHmmssfffff") + random.Next(10000, 99999).ToString();
            string Header =
                "<Message>" +
                "<Header>" +
                "<MESSAGENAME>EAP_ALIVE_CHECK</MESSAGENAME>" +
                "<TRANSACTIONID>" + transactionID + "</TRANSACTIONID>" +
                "<REPLYSUBJECTNAME>" + ip + "</REPLYSUBJECTNAME>" +
                "</Header>";
            string Body =
                "<Body>" +
                "<EquipmentID>" + device.DeviceID + " </EquipmentID> " +
                "</Body>";
            string Return =
                "<Return>" +
                "<ReturnCode></ReturnCode>" +
                "<ReturnMessage></ReturnMessage>" +
                "</Return>" +
                "</Message>";

            MSG = (Header + Body + Return).Replace(" ", "");
            MSG = FormatXML(MSG);
            byte[] buffer = Encoding.UTF8.GetBytes(MSG);

            string num = Convert.ToString(buffer.Length, 16);
            string head = string.Empty;
            for (int i = 0; i < 8 - num.Length; i++)
            {
                head += "0";
            }
            head += num.ToUpper();

            //定义一个string类型的变量，用于存储转换后的值。
            string resultMSG = string.Empty;

            for (int i = 0; i < buffer.Length; i++)
            {
                //将每一个字节数组转换成16进制的字符串，以空格相隔开。
                string convert = Convert.ToString(buffer[i], 16);
                if (convert.Length == 1)
                {
                    convert = convert.Insert(0, "0");

                }
                resultMSG += convert.ToUpper();
            }
            resultMSG = head + resultMSG;

            byte[] sendByte = ConvertHexStrToByteArray(resultMSG);

            return sendByte;

        }

        /// <summary>
        ///收到指令后的回复行为
        /// </summary>
        /// <param name="device"></param>
        /// <param name="MESSAGENAME"></param>
        /// <param name="TRANSACTIONID"></param>
        /// <param name="ReturnCode"></param>
        /// <param name="ReturnMessage"></param>
        /// <param name="MSG"></param>
        /// <returns></returns>
        public static byte[] ReturnMsg(Device device, string MESSAGENAME, string TRANSACTIONID, string ReturnCode, string ReturnMessage, out string MSG)
        {
            string ip = device.TCPClient.ClientSocket.LocalEndPoint.ToString();
            string Header =
                "<Message>" +
                "<Header>" +
                "<MESSAGENAME>" + MESSAGENAME + "</MESSAGENAME>" +
                "<TRANSACTIONID>" + TRANSACTIONID + "</TRANSACTIONID>" +
                "<REPLYSUBJECTNAME>" + ip + "</REPLYSUBJECTNAME>" +
                "</Header>";
            string Body =
                "<Body>" +
                "<EquipmentID> " + device.DeviceID + "</EquipmentID> " +
                "</Body>";
            string Return =
                "<Return>" +
                "<ReturnCode>" + ReturnCode + "</ReturnCode>" +
                "<ReturnMessage>" + ReturnMessage + "</ReturnMessage>" +
                "</Return>" +
                "</Message>";

            MSG = (Header + Body + Return).Replace(" ", "");
            MSG = FormatXML(MSG);
            byte[] buffer = Encoding.UTF8.GetBytes(MSG);

            string num = Convert.ToString(buffer.Length, 16);
            string head = string.Empty;
            for (int i = 0; i < 8 - num.Length; i++)
            {
                head += "0";
            }
            head += num.ToUpper();

            //定义一个string类型的变量，用于存储转换后的值。
            string resultMSG = string.Empty;

            for (int i = 0; i < buffer.Length; i++)
            {
                //将每一个字节数组转换成16进制的字符串，以空格相隔开。
                string convert = Convert.ToString(buffer[i], 16);
                if (convert.Length == 1)
                {
                    convert = convert.Insert(0, "0");

                }
                resultMSG += convert.ToUpper();
            }
            resultMSG = head + resultMSG;

            byte[] sendByte = ConvertHexStrToByteArray(resultMSG);

            return sendByte;
        }

        /// <summary>
        /// 产品移入设备上报
        /// </summary>
        /// <param name="device"></param>
        /// <param name="JobID"></param>
        /// <param name="JobType"></param>
        /// <param name="MSG"></param>
        /// <param name="transactionID"></param>
        /// <returns></returns>
        public static byte[] JOB_MOVE_IN_REQUEST(Device device, string JobID, string JobType, out string MSG, out string transactionID)
        {
            Random random = new Random();
            string ip = device.TCPClient.ClientSocket.LocalEndPoint.ToString();
            transactionID = DateTime.Now.ToString("yyyyMMddHHmmssfffff") + random.Next(10000, 99999).ToString();
            string Header =
                "<Message>" +
                "<Header>" +
                "<MESSAGENAME>JOB_MOVE_IN_REQUEST</MESSAGENAME>" +
                "<TRANSACTIONID>" + transactionID + "</TRANSACTIONID>" +
                "<REPLYSUBJECTNAME>" + ip + "</REPLYSUBJECTNAME>" +
                "</Header>";
            string Body =
                "<Body>" +
                "<EquipmentID>" + device.DeviceID + " </EquipmentID> " +
                "<JobID>" + JobID + " </JobID> " +
                "<JobType>" + JobType + " </JobType> " +
                "</Body>";
            string Return =
                "<Return>" +
                "<ReturnCode></ReturnCode>" +
                "<ReturnMessage></ReturnMessage>" +
                "</Return>" +
                "</Message>";

            MSG = (Header + Body + Return).Replace(" ", "");
            MSG = FormatXML(MSG);
            byte[] buffer = Encoding.UTF8.GetBytes(MSG);

            string num = Convert.ToString(buffer.Length, 16);
            string head = string.Empty;
            for (int i = 0; i < 8 - num.Length; i++)
            {
                head += "0";
            }
            head += num.ToUpper();

            //定义一个string类型的变量，用于存储转换后的值。
            string resultMSG = string.Empty;

            for (int i = 0; i < buffer.Length; i++)
            {
                //将每一个字节数组转换成16进制的字符串，以空格相隔开。
                string convert = Convert.ToString(buffer[i], 16);
                if (convert.Length == 1)
                {
                    convert = convert.Insert(0, "0");

                }
                resultMSG += convert.ToUpper();
            }
            resultMSG = head + resultMSG;

            byte[] sendByte = ConvertHexStrToByteArray(resultMSG);

            return sendByte;

        }

        /// <summary>
        /// 切换配方后上报
        /// </summary>
        /// <param name="device"></param>
        /// <param name="product"></param>
        /// <param name="MSG"></param>
        /// <param name="transactionID"></param>
        /// <returns></returns>
        public static byte[] CURRENT_RECIPE_ID_REPORT(Device device, out string MSG, out string transactionID)
        {
            Random random = new Random();
            string ip = device.TCPClient.ClientSocket.LocalEndPoint.ToString();
            transactionID = DateTime.Now.ToString("yyyyMMddHHmmssfffff") + random.Next(10000, 99999).ToString();
            string Header =
                "<Message>" +
                "<Header>" +
                "<MESSAGENAME>CURRENT_RECIPE_ID_REPORT</MESSAGENAME>" +
                "<TRANSACTIONID>" + transactionID + "</TRANSACTIONID>" +
                "<REPLYSUBJECTNAME>" + ip + "</REPLYSUBJECTNAME>" +
                "</Header>";

            string RecipeParameterList = "";

            foreach (var item in Product.RecipeParameterList)
            {
                RecipeParameterList +=
                    "<RecipeParameter>" +
                    "<Name>" + item.Key + " </Name> " +
                    "<Value>" + Product.RecipeParameterList[item.Key] + " </Value> " +
                    "</RecipeParameter>";
            }
            string Body =
                "<Body>" +
                "<EquipmentID>" + device.DeviceID + " </EquipmentID> " +
                "<RecipeID>" + Product.RecipeID + " </RecipeID> " +
                "<RecipeVersion>" + Product.RecipeVersion + " </RecipeVersion> " +
                "<RecipeParameterList>" +
                  RecipeParameterList +
                "</RecipeParameterList> " +
                "</Body>";
            string Return =
                "<Return>" +
                "<ReturnCode></ReturnCode>" +
                "<ReturnMessage></ReturnMessage>" +
                "</Return>" +
                "</Message>";

            MSG = (Header + Body + Return).Replace(" ", "");
            MSG = FormatXML(MSG);
            byte[] buffer = Encoding.UTF8.GetBytes(MSG);

            string num = Convert.ToString(buffer.Length, 16);
            string head = string.Empty;
            for (int i = 0; i < 8 - num.Length; i++)
            {
                head += "0";
            }
            head += num.ToUpper();

            //定义一个string类型的变量，用于存储转换后的值。
            string resultMSG = string.Empty;

            for (int i = 0; i < buffer.Length; i++)
            {
                //将每一个字节数组转换成16进制的字符串，以空格相隔开。
                string convert = Convert.ToString(buffer[i], 16);
                if (convert.Length == 1)
                {
                    convert = convert.Insert(0, "0");

                }
                resultMSG += convert.ToUpper();
            }
            resultMSG = head + resultMSG;

            byte[] sendByte = ConvertHexStrToByteArray(resultMSG);

            return sendByte;

        }

        /// <summary>
        /// 配方修改请求
        /// </summary>
        /// <param name="device"></param>
        /// <param name="product"></param>
        /// <param name="ModifyFlag"></param>
        /// <param name="MSG"></param>
        /// <param name="transactionID"></param>
        /// <returns></returns>
        public static byte[] RECIPE_MODIFY_REPORT(Device device, string ModifyFlag, out string MSG, out string transactionID)
        {
            //ModifyFlag
            //1：Create
            //2：Modify
            //3：Delete
            Random random = new Random();
            string ip = device.TCPClient.ClientSocket.LocalEndPoint.ToString();
            transactionID = DateTime.Now.ToString("yyyyMMddHHmmssfffff") + random.Next(10000, 99999).ToString();
            string Header =
                "<Message>" +
                "<Header>" +
                "<MESSAGENAME>RECIPE_MODIFY_REPORT</MESSAGENAME>" +
                "<TRANSACTIONID>" + transactionID + "</TRANSACTIONID>" +
                "<REPLYSUBJECTNAME>" + ip + "</REPLYSUBJECTNAME>" +
                "</Header>";

            string RecipeParameterList = "";
            foreach (var item in Product.RecipeParameterList)
            {
                RecipeParameterList +=
                    "<RecipeParameter>" +
                    "<Name>" + item.Key + " </Name> " +
                    "<Value>" + Product.RecipeParameterList[item.Key] + " </Value> " +
                    "</RecipeParameter>";
            }

            string Body =
                "<Body>" +
                "<EquipmentID>" + device.DeviceID + " </EquipmentID> " +
                "<RecipeID>" + Product.RecipeID + " </RecipeID> " +
                "<RecipeVersion>" + Product.RecipeVersion + " </RecipeVersion> " +
                "<ModifyFlag>" + ModifyFlag + " </ModifyFlag> " +
                "<RecipeParameterList>" +
                  RecipeParameterList +
                "</RecipeParameterList> " +
                "</Body>";
            string Return =
                "<Return>" +
                "<ReturnCode></ReturnCode>" +
                "<ReturnMessage></ReturnMessage>" +
                "</Return>" +
                "</Message>";

            MSG = (Header + Body + Return).Replace(" ", "");
            MSG = FormatXML(MSG);
            byte[] buffer = Encoding.UTF8.GetBytes(MSG);

            string num = Convert.ToString(buffer.Length, 16);
            string head = string.Empty;
            for (int i = 0; i < 8 - num.Length; i++)
            {
                head += "0";
            }
            head += num.ToUpper();

            //定义一个string类型的变量，用于存储转换后的值。
            string resultMSG = string.Empty;

            for (int i = 0; i < buffer.Length; i++)
            {
                //将每一个字节数组转换成16进制的字符串，以空格相隔开。
                string convert = Convert.ToString(buffer[i], 16);
                if (convert.Length == 1)
                {
                    convert = convert.Insert(0, "0");

                }
                resultMSG += convert.ToUpper();
            }
            resultMSG = head + resultMSG;

            byte[] sendByte = ConvertHexStrToByteArray(resultMSG);

            return sendByte;

        }

        public static byte[] PORT_STATUS_REPORT(Device device, string PortNo, string PortStatus, string CarrierID, out string MSG, out string transactionID)
        {
            Random random = new Random();
            string ip = device.TCPClient.ClientSocket.LocalEndPoint.ToString();
            transactionID = DateTime.Now.ToString("yyyyMMddHHmmssfffff") + random.Next(10000, 99999).ToString();
            string Header =
                "<Message>" +
                "<Header>" +
                "<MESSAGENAME>PORT_STATUS_REPORT</MESSAGENAME>" +
                "<TRANSACTIONID>" + transactionID + "</TRANSACTIONID>" +
                "<REPLYSUBJECTNAME>" + ip + "</REPLYSUBJECTNAME>" +
                "</Header>";
            string Body =
                "<Body>" +
                "<EquipmentID>" + device.DeviceID + " </EquipmentID> " +
                "<PortNo>" + PortNo + " </PortNo> " +
                "<PortStatus>" + PortStatus + " </PortStatus> " +
                  "<CarrierID>" + CarrierID + " </CarrierID> " +
                "</Body>";
            string Return =
                "<Return>" +
                "<ReturnCode></ReturnCode>" +
                "<ReturnMessage></ReturnMessage>" +
                "</Return>" +
                "</Message>";

            MSG = (Header + Body + Return).Replace(" ", "");
            MSG = FormatXML(MSG);
            byte[] buffer = Encoding.UTF8.GetBytes(MSG);

            string num = Convert.ToString(buffer.Length, 16);
            string head = string.Empty;
            for (int i = 0; i < 8 - num.Length; i++)
            {
                head += "0";
            }
            head += num.ToUpper();

            //定义一个string类型的变量，用于存储转换后的值。
            string resultMSG = string.Empty;

            for (int i = 0; i < buffer.Length; i++)
            {
                //将每一个字节数组转换成16进制的字符串，以空格相隔开。
                string convert = Convert.ToString(buffer[i], 16);
                if (convert.Length == 1)
                {
                    convert = convert.Insert(0, "0");

                }
                resultMSG += convert.ToUpper();
            }
            resultMSG = head + resultMSG;

            byte[] sendByte = ConvertHexStrToByteArray(resultMSG);

            return sendByte;

        }
        public static byte[] ICT_JOB_SEND_REPORT(Device device, List<string> Info, string JobID, string JobJudge, out string MSG, out string transactionID)
        {
            //ModifyFlag
            //1：Create
            //2：Modify
            //3：Delete
            Random random = new Random();
            string ip = device.TCPClient.ClientSocket.LocalEndPoint.ToString();
            transactionID = DateTime.Now.ToString("yyyyMMddHHmmssfffff") + random.Next(10000, 99999).ToString();
            string Header =
                "<Message>" +
                "<Header>" +
                "<MESSAGENAME>JOB_SEND_REPORT</MESSAGENAME>" +
                "<TRANSACTIONID>" + transactionID + "</TRANSACTIONID>" +
                "<REPLYSUBJECTNAME>" + ip + "</REPLYSUBJECTNAME>" +
                "</Header>";

            int MulBrdNum = Convert.ToInt32(Info[11]);
            string restList = Info[12];
            string MulBrdResultList = "";
            string OXInformation = "";

            Dictionary<string, string> MulBrdResult = new Dictionary<string, string>()
            {
                {"0","Reserved"},
                {"1", "Contact Test" },
                {"2" , "Open" },
                {"3" , "Short" },
                {"4" , "Resistor" },
                {"5" , "Capacitor" },
                {"6" , "Inductor" },
                {"7" , "Diode" },
                {"8" , "Transistor and Socket" },
                {"9" , "Clamping Diode"  },
                {"10" , "IC Open" },
                {"11" , "Tree Test" },
                {"12" , "Voltage Test" },
                {"13" , "Other on Power Test" },
                {"14" , "Frequency Test" },
                {"15" , "On-Board_Programming" },
                {"16" , "Function" },
            };

            for (int i = 0; i < MulBrdNum; i++)
            {
                if (restList[i] == 'P')
                {
                    OXInformation += "O";
                    continue;
                }
                else
                {
                    OXInformation += "X";
                }

                string DetailResult = Info[13 + i];
                int Code = 0;
                for (int p = 2; p < DetailResult.Length; p++)
                {
                    if (DetailResult[p] == 'F')
                    {
                        if (p == 2)
                        {
                            Code = 1;
                        }
                        else if (p == 3)
                        {
                            Code = 2;
                        }
                        else if (p > 3)
                        {
                            Code = 3;
                        }
                        string MulBrdResultMSG =
                            "<DefectData>" +
                            "<DefectLocation>" +
                            "0" + (i + 1) + "--" +
                            "</DefectLocation> " +
                            "<DefectCode>" +
                            Code.ToString() +
                             "</DefectCode> " +
                             "<DefectValue>" +
                             MulBrdResult[p.ToString()] +
                             "</DefectValue> " +
                             "</DefectData> ";
                        MulBrdResultList += MulBrdResultMSG;
                    }
                }
            }



            string Body =
                "<Body>" +
                "<EquipmentID>" + device.DeviceID + " </EquipmentID> " +
                "<JobID>" + JobID + " </JobID> " +
                "<JobJudge>" + JobJudge + " </JobJudge> " +
                "<OXInformation>" + OXInformation + " </OXInformation> " +
                "<DefectDataList>" +
                 MulBrdResultList +
                "</DefectDataList> " +
                "</Body>";
            string Return =
                "<Return>" +
                "<ReturnCode></ReturnCode>" +
                "<ReturnMessage></ReturnMessage>" +
                "</Return>" +
                "</Message>";



            MSG = (Header + Body + Return).Replace(" ", "");
            MSG = FormatXML(MSG);
            byte[] buffer = Encoding.UTF8.GetBytes(MSG);

            string num = Convert.ToString(buffer.Length, 16);
            string head = string.Empty;
            for (int i = 0; i < 8 - num.Length; i++)
            {
                head += "0";
            }
            head += num.ToUpper();

            //定义一个string类型的变量，用于存储转换后的值。
            string resultMSG = string.Empty;

            for (int i = 0; i < buffer.Length; i++)
            {
                //将每一个字节数组转换成16进制的字符串，以空格相隔开。
                string convert = Convert.ToString(buffer[i], 16);
                if (convert.Length == 1)
                {
                    convert = convert.Insert(0, "0");

                }
                resultMSG += convert.ToUpper();
            }
            resultMSG = head + resultMSG;

            byte[] sendByte = ConvertHexStrToByteArray(resultMSG);

            return sendByte;

        }
        //public static byte[] ICT_JOB_SEND_REPORT(Device device, List<string> Info, string JobID, string JobJudge, out string MSG, out string transactionID)
        //{
        //    //ModifyFlag
        //    //1：Create
        //    //2：Modify
        //    //3：Delete
        //    Random random = new Random();
        //    string ip = device.TCPClient.ClientSocket.LocalEndPoint.ToString();
        //    transactionID = DateTime.Now.ToString("yyyyMMddHHmmssfffff") + random.Next(10000, 99999).ToString();
        //    string Header =
        //        "<Message>" +
        //        "<Header>" +
        //        "<MESSAGENAME>JOB_SEND_REPORT</MESSAGENAME>" +
        //        "<TRANSACTIONID>" + transactionID + "</TRANSACTIONID>" +
        //        "<REPLYSUBJECTNAME>" + ip + "</REPLYSUBJECTNAME>" +
        //        "</Header>";

        //    int MulBrdNum = Convert.ToInt32(Info[11]);
        //    string restList = Info[12];
        //    string MulBrdResultList = "";
        //    string OXInformation = "";
        //    for (int i = 0; i < MulBrdNum; i++)
        //    {
        //        if (restList[i] == 'P')
        //        {
        //            OXInformation += "O";
        //            continue;
        //        }
        //        else
        //        {
        //            OXInformation += "X";
        //        }
        //        Dictionary<string, char> MulBrdResult = new Dictionary<string, char>()
        //    {
        //        {"Reserved",Info[13+i][0] },
        //        {"Contact Test",Info[13+i][1] },
        //        {"Open",Info[13+i][2] },
        //        {"Short",Info[13+i][3] },
        //        {"Resistor",Info[13+i][4] },
        //        {"Capacitor",Info[13+i][5] },
        //        {"Inductor",Info[13+i][6] },
        //        {"Diode",Info[13+i][7] },
        //        {"Transistor and Socket",Info[13+i][8] },
        //        {"Clamping Diode",Info[13+i][9] },
        //        {"IC Open",Info[13+i][10] },
        //        {"Tree Test",Info[13+i][11] },
        //        {"Voltage Test",Info[13+i][12] },
        //        {"Other on Power Test",Info[13+i][13] },
        //        {"Frequency Test",Info[13+i][14] },
        //        {"On-Board_Programming",Info[13+i][15] },
        //        {"Function",Info[13+i][16] },
        //    };
        //        string value = JsonConvert.SerializeObject(MulBrdResult);
        //        string MulBrdResultMSG =
        //            "<DefectData>" +
        //         "<DefectLocation>" +
        //         "0" + (i + 1) + "--" +
        //        "</DefectLocation> " +
        //         "<DefectCode>" +

        //        "</DefectCode> " +
        //         "<DefectValue>" +
        //          value +
        //        "</DefectValue> " +
        //        "</DefectData> ";
        //        MulBrdResultList += MulBrdResultMSG;
        //    }



        //    string Body =
        //        "<Body>" +
        //        "<EquipmentID>" + device.DeviceID + " </EquipmentID> " +
        //        "<JobID>" + JobID + " </JobID> " +
        //        "<JobJudge>" + JobJudge + " </JobJudge> " +
        //        "<OXInformation>" + OXInformation + " </OXInformation> " +
        //        "<DefectDataList>" +
        //         MulBrdResultList +
        //        "</DefectDataList> " +
        //        "</Body>";
        //    string Return =
        //        "<Return>" +
        //        "<ReturnCode></ReturnCode>" +
        //        "<ReturnMessage></ReturnMessage>" +
        //        "</Return>" +
        //        "</Message>";



        //    MSG = (Header + Body + Return).Replace(" ", "");
        //    MSG = FormatXML(MSG);
        //    byte[] buffer = Encoding.UTF8.GetBytes(MSG);

        //    string num = Convert.ToString(buffer.Length, 16);
        //    string head = string.Empty;
        //    for (int i = 0; i < 8 - num.Length; i++)
        //    {
        //        head += "0";
        //    }
        //    head += num.ToUpper();

        //    //定义一个string类型的变量，用于存储转换后的值。
        //    string resultMSG = string.Empty;

        //    for (int i = 0; i < buffer.Length; i++)
        //    {
        //        //将每一个字节数组转换成16进制的字符串，以空格相隔开。
        //        string convert = Convert.ToString(buffer[i], 16);
        //        if (convert.Length == 1)
        //        {
        //            convert = convert.Insert(0, "0");

        //        }
        //        resultMSG += convert.ToUpper();
        //    }
        //    resultMSG = head + resultMSG;

        //    byte[] sendByte = ConvertHexStrToByteArray(resultMSG);

        //    return sendByte;

        //}
        public static byte[] EDC_REPORT(Device device, EDC_REPORT EDC_REPORT, string ProcessTime, string ProcessStartTime, string ProcessEndTime, out string MSG, out string transactionID)
        {
            //ModifyFlag
            //1：Create
            //2：Modify
            //3：Delete
            Random random = new Random();
            string ip = "";// device.TCPClient.ClientSocket.LocalEndPoint.ToString();
            transactionID = DateTime.Now.ToString("yyyyMMddHHmmssfffff") + random.Next(10000, 99999).ToString();
            string Header =
                "<Message>" +
                "<Header>" +
                "<MESSAGENAME>EDC_REPORT</MESSAGENAME>" +
                "<TRANSACTIONID>" + transactionID + "</TRANSACTIONID>" +
                "<REPLYSUBJECTNAME>" + ip + "</REPLYSUBJECTNAME>" +
                "</Header>";

            string Body =
                  "<Body>" +
                  "<EquipmentID>" + device.DeviceID + " </EquipmentID> " +
                  "<JobID>" + EDC_REPORT.JobID + " </JobID> " +
                  "<ProcessTime>" + ProcessTime + " </ProcessTime> " +
                  "<ProcessStartTime>" + ProcessStartTime + " </ProcessStartTime> " +
                  "<ProcessEndTime>" + ProcessEndTime + " </ProcessEndTime> " +
                  "<ProcessDataList> " +
                    "<ProcessData> " +
                     "<Name> " + "ProductType" + "</Name> " +
                     "<Value> " + EDC_REPORT.ProductType + "</Value> " +
                    "</ProcessData> " +
                     "<ProcessData> " +
                     "<Name> " + "ProcessMode" + "</Name> " +
                     "<Value> " + EDC_REPORT.ProcessMode + "</Value> " +
                    "</ProcessData> " +
                     "<ProcessData> " +
                     "<Name> " + "PCBQuantity" + "</Name> " +
                     "<Value> " + EDC_REPORT.PCBQuantity + "</Value> " +
                    "</ProcessData> " +
                     "<ProcessData> " +
                     "<Name> " + "PCBBarcode" + "</Name> " +
                     "<Value> " + EDC_REPORT.PCBBarcode + "</Value> " +
                    "</ProcessData> " +
                     "<ProcessData> " +
                     "<Name> " + "InitialPassQuantity" + "</Name> " +
                     "<Value> " + EDC_REPORT.InitialPassQuantity + "</Value> " +
                    "</ProcessData> " +
                     "<ProcessData> " +
                     "<Name> " + "CheckResult" + "</Name> " +
                     "<Value> " + EDC_REPORT.CheckResult + "</Value> " +
                    "</ProcessData> " +
                     "<ProcessData> " +
                     "<Name> " + "EffectiveTime" + "</Name> " +
                     "<Value> " + EDC_REPORT.EffectiveTime + "</Value> " +
                    "</ProcessData> " +
                     "<ProcessData> " +
                     "<Name> " + "AuxiliaryTime" + "</Name> " +
                     "<Value> " + EDC_REPORT.AuxiliaryTime + "</Value> " +
                    "</ProcessData> " +
                     "<ProcessData> " +
                     "<Name> " + "WaitingTime" + "</Name> " +
                     "<Value> " + EDC_REPORT.WaitingTime + "</Value> " +
                    "</ProcessData> " +
                  "</ProcessDataList> " +
                  "</Body>";
            string Return =
                "<Return>" +
                "<ReturnCode></ReturnCode>" +
                "<ReturnMessage></ReturnMessage>" +
                "</Return>" +
                "</Message>";



            MSG = (Header + Body + Return).Replace(" ", "");
            MSG = FormatXML(MSG);
            byte[] buffer = Encoding.UTF8.GetBytes(MSG);

            string num = Convert.ToString(buffer.Length, 16);
            string head = string.Empty;
            for (int i = 0; i < 8 - num.Length; i++)
            {
                head += "0";
            }
            head += num.ToUpper();

            //定义一个string类型的变量，用于存储转换后的值。
            string resultMSG = string.Empty;

            for (int i = 0; i < buffer.Length; i++)
            {
                //将每一个字节数组转换成16进制的字符串，以空格相隔开。
                string convert = Convert.ToString(buffer[i], 16);
                if (convert.Length == 1)
                {
                    convert = convert.Insert(0, "0");

                }
                resultMSG += convert.ToUpper();
            }
            resultMSG = head + resultMSG;

            byte[] sendByte = ConvertHexStrToByteArray(resultMSG);

            return sendByte;

        }
        public static byte[] FCT_JOB_SEND_REPORT(Device device, EDC_REPORT EDC_REPORT, List<string> Info, out string MSG, out string transactionID)
        {
            //ModifyFlag
            //1：Create
            //2：Modify
            //3：Delete
            Random random = new Random();
            string ip = device.TCPClient.ClientSocket.LocalEndPoint.ToString();
            transactionID = DateTime.Now.ToString("yyyyMMddHHmmssfffff") + random.Next(10000, 99999).ToString();
            string Header =
                "<Message>" +
                "<Header>" +
                "<MESSAGENAME>JOB_SEND_REPORT</MESSAGENAME>" +
                "<TRANSACTIONID>" + transactionID + "</TRANSACTIONID>" +
                "<REPLYSUBJECTNAME>" + ip + "</REPLYSUBJECTNAME>" +
                "</Header>";

            int MulBrdNum = Convert.ToInt32(EDC_REPORT.PCBQuantity);

            string MulBrdResultList = "";
            string OXInformation = "";
            for (int i = 0; i < MulBrdNum; i++)
            {
                if (Info[10 + i * 10] == "PASS")
                {
                    OXInformation += "O";
                    continue;
                }
                else
                {
                    OXInformation += "X";
                }
                Dictionary<string, string> MulBrdResult = new Dictionary<string, string>()
            {
                {"输入功率",Info[7 + i * 10] },
            };
                string value = JsonConvert.SerializeObject(MulBrdResult);
                string MulBrdResultMSG =
                    "<DefectData>" +
                 "<DefectLocation>" +
                 "0" + (i + 1) + "--" +
                "</DefectLocation> " +
                 "<DefectCode>" +

                "</DefectCode> " +
                 "<DefectValue>" +
                  value +
                "</DefectValue> " +
                "</DefectData> ";
                MulBrdResultList += MulBrdResultMSG;
            }



            string Body =
                "<Body>" +
                "<EquipmentID>" + device.DeviceID + " </EquipmentID> " +
                "<JobID>" + EDC_REPORT.JobID + " </JobID> " +
                "<JobJudge>" + EDC_REPORT.Pass + " </JobJudge> " +
                "<OXInformation>" + OXInformation + " </OXInformation> " +
                "<DefectDataList>" +
                 MulBrdResultList +
                "</DefectDataList> " +
                "</Body>";
            string Return =
                "<Return>" +
                "<ReturnCode></ReturnCode>" +
                "<ReturnMessage></ReturnMessage>" +
                "</Return>" +
                "</Message>";



            MSG = (Header + Body + Return).Replace(" ", "");
            MSG = FormatXML(MSG);
            byte[] buffer = Encoding.UTF8.GetBytes(MSG);

            string num = Convert.ToString(buffer.Length, 16);
            string head = string.Empty;
            for (int i = 0; i < 8 - num.Length; i++)
            {
                head += "0";
            }
            head += num.ToUpper();

            //定义一个string类型的变量，用于存储转换后的值。
            string resultMSG = string.Empty;

            for (int i = 0; i < buffer.Length; i++)
            {
                //将每一个字节数组转换成16进制的字符串，以空格相隔开。
                string convert = Convert.ToString(buffer[i], 16);
                if (convert.Length == 1)
                {
                    convert = convert.Insert(0, "0");

                }
                resultMSG += convert.ToUpper();
            }
            resultMSG = head + resultMSG;

            byte[] sendByte = ConvertHexStrToByteArray(resultMSG);

            return sendByte;

        }

        #region  格式转换
        /// <summary>
        /// string转XML
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string FormatXML(string str)
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(str);
            System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
            System.IO.StringWriter stringWriter = new System.IO.StringWriter(stringBuilder);
            System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter(stringWriter);
            xmlWriter.Formatting = System.Xml.Formatting.Indented;
            doc.WriteTo(xmlWriter);
            return stringBuilder.ToString();
        }
        /// <summary>
        /// 16位转byte[]
        /// </summary>
        /// <param name="hexStr"></param>
        /// <returns></returns>
        public static byte[] ConvertHexStrToByteArray(string hexStr)
        {
            string[] array = new string[hexStr.Length / 2];
            byte[] bytes = new byte[array.Length];
            for (int i = 0; i < hexStr.Length; i++)
            {
                array[i / 2] += hexStr[i];
            }
            for (int i = 0; i < array.Length; i++)
            {
                bytes[i] = Convert.ToByte(array[i], 16);
            }
            return bytes;
        }

        #endregion
    }
}
